///**
// * @file add_custom.cpp
// *
// * Copyright (C) 2024. Huawei Technologies Co., Ltd. All rights reserved.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// */
//#include "kernel_operator.h"
//#include "leakyrelu_custom_tilling.h"
//
//constexpr int32_t TOTAL_LENGTH = 8 * 2048;                            // total length of data
//constexpr int32_t USE_CORE_NUM = 8;                                   // num of core used
//constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;         // length computed of each core
////constexpr int32_t TILE_NUM = 8;                                       // split data into 8 tiles for each core
////constexpr int32_t BUFFER_NUM = 2;                                     // tensor num for each queue
////constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // separate to 2 parts, due to double buffer
//
//class KernelLeakyRelu {
//public:
//    __aicore__ inline KernelLeakyRelu() {}
//    __aicore__ inline void Init(GM_ADDR src, GM_ADDR dst, float negativeSlope, LeakyReluCustomTilingData tiling)
//    {
//        this->negativeSlope = static_cast<float>(negativeSlope);
//        this->blockLength = tiling.blockLength;
//        this->tileNum = tiling.tileNum;
//        this->tileLength = tiling.tileLength;
//
//        srcGm.SetGlobalBuffer((__gm__ float *)src + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
//        dstGm.SetGlobalBuffer((__gm__ float *)dst + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
//        pipe.InitBuffer(inQueueSrc, 1, this->tileLength * sizeof(float));
//        pipe.InitBuffer(tmpBuffer, this->tileLength * sizeof(float));
//        pipe.InitBuffer(outQueueDst, 1, this->tileLength * sizeof(float));
//    }
//    __aicore__ inline void Process()
//    {
//        int32_t loopCount = this->tileNum;
//        for (int32_t i = 0; i < loopCount; i++) {
//            CopyIn(i);
//            Compute(i);
//            CopyOut(i);
//        }
//    }
//
//private:
//    __aicore__ inline void CopyIn(int32_t progress)
//    {
//        AscendC::LocalTensor<float> srcLocal = inQueueSrc.AllocTensor<float>();
//        AscendC::DataCopy(srcLocal, srcGm[progress * this->tileLength], this->tileLength);
//        inQueueSrc.EnQue(srcLocal);
//    }
//    __aicore__ inline void Compute(int32_t progress)
//    {
//        AscendC::LocalTensor<float> srcLocal = inQueueSrc.DeQue<float>();
//        AscendC::LocalTensor<float> dstLocal = outQueueDst.AllocTensor<float>();
//        AscendC::LocalTensor<float> tmp = tmpBuffer.Get<float>();
//
//        AscendC::Muls(tmp, srcLocal, this->negativeSlope, this->tileLength);
//        AscendC::Max(dstLocal, tmp, srcLocal, this->tileLength);
//
//        outQueueDst.EnQue<float>(dstLocal);
//        inQueueSrc.FreeTensor(srcLocal);
//    }
//    __aicore__ inline void CopyOut(int32_t progress)
//    {
//        AscendC::LocalTensor<float> dstLocal = outQueueDst.DeQue<float>();
//        AscendC::DataCopy(dstGm[progress * this->tileLength], dstLocal, this->tileLength);
//        outQueueDst.FreeTensor(dstLocal);
//    }
//
//private:
//  	float negativeSlope;
//  	uint32_t blockLength;
//    uint32_t tileNum;
//    uint32_t tileLength;
//
//    AscendC::TPipe pipe;
//    AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrc;
//    AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueDst;
//    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuffer;
//
//    AscendC::GlobalTensor<float> srcGm;
//    AscendC::GlobalTensor<float> dstGm;
//};
//
//extern "C" __global__ __aicore__ void leakyrelu_custom(GM_ADDR src, GM_ADDR dst, float negativeSlope, LeakyReluCustomTilingData tilling)
//{
//    KernelLeakyRelu op;
//    op.Init(src, dst, negativeSlope, tilling);
//    op.Process();
//}
//


/**
 * @file add_custom.cpp
 *
 * Copyright (C) 2025. Huawei Technologies Co., Ltd. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#include "leakyrelu_custom_tiling.h"
#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

constexpr uint32_t CUSTOM_BFLOAT16 = 0;
constexpr uint32_t CUSTOM_FLOAT16 = 1;
constexpr uint32_t CUSTOM_FLOAT32 = 2;
constexpr uint32_t CUSTOM_INT8 = 3;
constexpr uint32_t CUSTOM_INT16 = 4;
constexpr uint32_t CUSTOM_INT32 = 5;

constexpr uint32_t LAST_TWO_TILE = 2;

template <typename dataType> class KernalLeakyRelu;
//template <> class KernalLeakyRelu <bfloat16_t> {
//public:
//    __aicore__ inline KernalLeakyRelu() {}
//    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, float negativeSlope, LeakyReluCustomTilingData tiling)
//    {
//        if (tiling.isEvenCore) {
//            this->blockLength = tiling.blockLength;
//            this->tileNum = tiling.tileNum;
//            this->tileLength = tiling.tileLength / BUFFER_NUM;
//            this->lastTileLength = tiling.lastTileLength;
//
//            xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//            zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//        } else {
//            if (AscendC::GetBlockIdx() < tiling.formerNum) {
//                this->tileNum = tiling.formerTileNum;
//                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
//                this->lastTileLength = tiling.formerLastTileLength;
//
//                xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
//                zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
//            } else {
//                this->tileNum = tiling.tailTileNum;
//                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
//                this->lastTileLength = tiling.tailLastTileLength;
//
//                xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + tiling.formerLength * tiling.formerNum +
//                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
//                zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + tiling.formerLength * tiling.formerNum +
//                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
//            }
//        }
//        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(bfloat16_t));
//        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(bfloat16_t));
//
//        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(float));
//        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(float));
//    }
//    __aicore__ inline void Process()
//    {
//        int32_t loopCount = this->tileNum * BUFFER_NUM;
//        for (int32_t i = 0; i < loopCount; i++) {
//            CopyIn(i);
//            Compute(i);
//            CopyOut(i);
//        }
//    }
//
//private:
//    __aicore__ inline void CopyIn(int32_t progress)
//    {
//        AscendC::LocalTensor<bfloat16_t> xLocal = inQueueX.AllocTensor<bfloat16_t>();
//        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
//            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
//                this->tileLength);
//                this->tileLength);
//        } else {
//            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
//        }
//        inQueueX.EnQue(xLocal);
//    }
//    __aicore__ inline void Compute(int32_t progress)
//    {
//        AscendC::LocalTensor<bfloat16_t> xLocal = inQueueX.DeQue<bfloat16_t>();
//        AscendC::LocalTensor<bfloat16_t> zLocal = outQueueZ.AllocTensor<bfloat16_t>();
//
//        AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
//        AscendC::LocalTensor<float> tmpTensor1 = tmpBuf1.Get<float>();
//
//        AscendC::Cast(tmpTensor0, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
//
//        AscendC::Add(tmpTensor0, tmpTensor0, tmpTensor1, this->tileLength);
//        AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_RINT, this->tileLength);
//
//        outQueueZ.EnQue<bfloat16_t>(zLocal);
//        inQueueX.FreeTensor(xLocal);
//    }
//    __aicore__ inline void CopyOut(int32_t progress)
//    {
//        AscendC::LocalTensor<bfloat16_t> zLocal = outQueueZ.DeQue<bfloat16_t>();
//        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
//            AscendC::DataCopy(zGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength], zLocal,
//                this->tileLength);
//        } else {
//            AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
//        }
//        outQueueZ.FreeTensor(zLocal);
//    }
//
//private:
//    AscendC::TPipe pipe;
//    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
//    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
//    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
//    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
//    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
//
//    AscendC::GlobalTensor<bfloat16_t> xGm;
//    AscendC::GlobalTensor<bfloat16_t> yGm;
//    AscendC::GlobalTensor<bfloat16_t> zGm;
//
//    uint32_t blockLength;
//    uint32_t tileNum;
//    uint32_t tileLength;
//    uint32_t lastTileLength;
//};
//
//template <> class KernalLeakyRelu <int8_t> {
//public:
//    __aicore__ inline KernalLeakyRelu() {}
//    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, float negativeSlope, LeakyReluCustomTilingData tiling)
//    {
//        if (tiling.isEvenCore) {
//            this->blockLength = tiling.blockLength;
//            this->tileNum = tiling.tileNum;
//            this->tileLength = tiling.tileLength / BUFFER_NUM;
//            this->lastTileLength = tiling.lastTileLength;
//
//            xGm.SetGlobalBuffer((__gm__ int8_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//            zGm.SetGlobalBuffer((__gm__ int8_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//        } else {
//            if (AscendC::GetBlockIdx() < tiling.formerNum) {
//                this->tileNum = tiling.formerTileNum;
//                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
//                this->lastTileLength = tiling.formerLastTileLength;
//
//                xGm.SetGlobalBuffer((__gm__ int8_t *)x + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
//                zGm.SetGlobalBuffer((__gm__ int8_t *)z + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
//            } else {
//                this->tileNum = tiling.tailTileNum;
//                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
//                this->lastTileLength = tiling.tailLastTileLength;
//
//                xGm.SetGlobalBuffer((__gm__ int8_t *)x + tiling.formerLength * tiling.formerNum +
//                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
//                zGm.SetGlobalBuffer((__gm__ int8_t *)z + tiling.formerLength * tiling.formerNum +
//                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
//            }
//        }
//        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(int8_t));
//        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(int8_t));
//
//        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(half));
//        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(half));
//    }
//    __aicore__ inline void Process()
//    {
//        int32_t loopCount = this->tileNum * BUFFER_NUM;
//        for (int32_t i = 0; i < loopCount; i++) {
//            CopyIn(i);
//            Compute(i);
//            CopyOut(i);
//        }
//    }
//
//private:
//    __aicore__ inline void CopyIn(int32_t progress)
//    {
//        AscendC::LocalTensor<int8_t> xLocal = inQueueX.AllocTensor<int8_t>();
//        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
//            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
//                this->tileLength);
//        } else {
//            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
//        }
//        inQueueX.EnQue(xLocal);
//    }
//    __aicore__ inline void Compute(int32_t progress)
//    {
//        AscendC::LocalTensor<int8_t> xLocal = inQueueX.DeQue<int8_t>();
//        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.AllocTensor<int8_t>();
//
//        AscendC::LocalTensor<half> tmpTensor0 = tmpBuf0.Get<half>();
//        AscendC::LocalTensor<half> tmpTensor1 = tmpBuf1.Get<half>();
//
//        AscendC::Cast(tmpTensor0, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
//
//        AscendC::Add(tmpTensor0, tmpTensor0, tmpTensor1, this->tileLength);
//        AscendC::Cast(zLocal, tmpTensor0, AscendC::RoundMode::CAST_NONE, this->tileLength);
//
//        outQueueZ.EnQue<int8_t>(zLocal);
//        inQueueX.FreeTensor(xLocal);
//    }
//    __aicore__ inline void CopyOut(int32_t progress)
//    {
//        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.DeQue<int8_t>();
//        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
//            AscendC::DataCopy(zGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength], zLocal,
//                this->tileLength);
//        } else {
//            AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
//        }
//        outQueueZ.FreeTensor(zLocal);
//    }
//
//private:
//    AscendC::TPipe pipe;
//    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
//    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
//    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
//    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
//    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
//
//    AscendC::GlobalTensor<int8_t> xGm;
//    AscendC::GlobalTensor<int8_t> yGm;
//    AscendC::GlobalTensor<int8_t> zGm;
//
//    uint32_t blockLength;
//    uint32_t tileNum;
//    uint32_t tileLength;
//    uint32_t lastTileLength;
//};

template <typename dataType> class KernalLeakyRelu {
public:
    __aicore__ inline KernalLeakyRelu() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, float negativeSlope, LeakyReluCustomTilingData tiling)
    {
        this->negativeSlope = negativeSlope;
        if (tiling.isEvenCore) {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ dataType *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ dataType *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        } else {
            if (AscendC::GetBlockIdx() < tiling.formerNum) {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ dataType *)x + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ dataType *)z + tiling.formerLength * AscendC::GetBlockIdx(), tiling.formerLength);
            } else {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ dataType *)x + tiling.formerLength * tiling.formerNum + 
                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ dataType *)z + tiling.formerLength * tiling.formerNum + 
                    tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum), tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(dataType));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(dataType));
        pipe.InitBuffer(tmpBuffer, this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<dataType> xLocal = inQueueX.AllocTensor<dataType>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                this->tileLength);
        } else {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<dataType> xLocal = inQueueX.DeQue<dataType>();
        AscendC::LocalTensor<dataType> zLocal = outQueueZ.AllocTensor<dataType>();
        AscendC::LocalTensor<float> tmp = tmpBuffer.Get<float>();

        AscendC::Muls(tmp, xLocal, this->negativeSlope, this->tileLength);
        AscendC::Max(zLocal, tmp, xLocal, this->tileLength);

        outQueueZ.EnQue<dataType>(zLocal);
        inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<dataType> zLocal = outQueueZ.DeQue<dataType>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1))) {
            AscendC::DataCopy(zGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength], zLocal,
                this->tileLength);
        } else {
            AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
        }
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuffer;

    AscendC::GlobalTensor<dataType> xGm;
    AscendC::GlobalTensor<dataType> zGm;

    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
    float negativeSlope;

};
        
extern "C" __global__ __aicore__ void leakyrelu_custom(GM_ADDR x, GM_ADDR z, float negativeSlope, LeakyReluCustomTilingData tiling)
{
//    KernalLeakyRelu<float> op;
//    op.Init(x, z, negativeSlope, tiling);
//    op.Process();
//    if (tiling.dataType == CUSTOM_BFLOAT16) {
//        KernalLeakyRelu<bfloat16_t> op;
//        op.Init(x, z, negativeSlope, tiling);
//        op.Process();
//    } else if (tiling.dataType == CUSTOM_FLOAT16) {
//        KernalLeakyRelu<half> op;
//        op.Init(x, z, negativeSlope, tiling);
//        op.Process();
//    }
    if (tiling.dataType == CUSTOM_FLOAT32) {
        KernalLeakyRelu<float> op;
        op.Init(x, z, negativeSlope, tiling);
        op.Process();
    }
//else if (tiling.dataType == CUSTOM_INT8) {
//        KernalLeakyRelu<int8_t> op;
//        op.Init(x, z, negativeSlope, tiling);
//        op.Process();
//    } else if (tiling.dataType == CUSTOM_INT16) {
//        KernalLeakyRelu<int16_t> op;
//        op.Init(x, z, negativeSlope, tiling);
//        op.Process();
//    } else if (tiling.dataType == CUSTOM_INT32) {
//        KernalLeakyRelu<int32_t> op;
//        op.Init(x, z, negativeSlope, tiling);
//        op.Process();
//    } else {
//        return;
//    }
}

